<?
/**
 * 上传文件处理
 * @author Administrator
 *
 */
class Model_UploadFile {
	
	private $session_info;
	
	private $save_basepath;
	private $savepath;
	private $hashpath;
	
	private $site_upload_path;
	
	private $attachment_type;
	private $resource_id;
	
	private $upload_type;
	
	private $_src_fileinfo;
	
	private $save_basepath_conf;
	
	/**
	 * 临时处理路径
	 */
	private $tmp_filesave_path;
	private $preview_filesave_path;
	
	function __construct($obj_id, $attachment_type, $upload_type = '_FILES') {
		$this->save_basepath_conf = array (
				// 新闻图片
				'content' => UPLOAD_PATH . DIRECTORY_SEPARATOR . 'content' . DIRECTORY_SEPARATOR,
				// 预览文件夹，该文件夹为临时文件夹，其中的文件会每天定期清理
				'preview' => UPLOAD_PATH . DIRECTORY_SEPARATOR . 'preview' . DIRECTORY_SEPARATOR 
		);
		// 临时文件夹
		$this->tmp_filesave_path = UPLOAD_PATH . DIRECTORY_SEPARATOR . 'tmp';
		$this->preview_filesave_path = UPLOAD_PATH . DIRECTORY_SEPARATOR . 'preview';
		
		if (! $obj_id) {
			return false;
		}
		
		$this->session_info = Zend_Registry::get ( 'CmsAdminSessionInfo' );
		$this->resource_id = $obj_id;
		
		if (! $this->session_info ['uid']) {
			return false;
		}
		
		if (! $this->setSavePath ( $attachment_type )) {
			return false;
		}
		
		$this->upload_type = $upload_type;
		
		$config = Zend_Registry::get ( 'config' );
		$this->site_upload_path = $config->view_conf->siteUploadPath;
		
		return true;
	}
	
	function setSavePath($attachment_type) {
		if (array_key_exists ( $attachment_type, $this->save_basepath_conf )) {
			$this->attachment_type = $attachment_type;
			$this->save_basepath = $this->save_basepath_conf [$attachment_type];
			$this->save_filename = $this->session_info ['uid'] . "_" . $this->resource_id . "_" . time ();
		} else {
			return false;
		}
		
		// 获取一个哈希目录
		$this->buildHashpath ();
		
		// 生成最终存储目录
		$this->savepath = $this->save_basepath . $this->hashpath;
		
		return $this->savepath;
	}
	
	function __get($name) {
		return $name;
	}
	
	private function buildHashpath() {
		$path_one = getHashPath ( time () . $this->session_info ['uid'], 101 );
		$path_two = getHashPath ( $path_one . time () );
		$path_one = formatHashPath ( $path_one );
		$path_two = formatHashPath ( $path_two );
		$this->hashpath = $path_one . DIRECTORY_SEPARATOR . $path_two;
		return $this->hashpath;
	}
	
	/**
	 * 预览图片
	 * 如果预览的图片是 $_FILES 提交过来，则将$_FILES 作为参数，但一定要是一个文件，而不是多个文件的数组！！！！
	 */
	function previewImg($src_filename, $size = null) {
		if ($this->upload_type == '_FILES') {
			$src_fileinfo = mime_contenttype ( $_FILES [$src_filename] ['name'] );
			$_tmp_savefile_name = $_FILES [$src_filename] ['tmp_name'];
			
			if (substr ( $src_fileinfo ['mime'], 0, 5 ) != 'image') {
				return false;
			}
			
			$savefile_name = $this->preview_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
			
			// 将PNG图片转换，放置在待处理文件夹内
			if ($src_fileinfo ['ext'] == "png" || $src_fileinfo ['ext'] == "bmp") {
				$_tmp_savefile_name = $this->tmp_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.jpg';
				$savefile_name = $this->preview_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.jpg';
				
				// 转换图片格式
				$img = new Imagick ( $_FILES [$src_filename] ['tmp_name'] );
				$img->setimagecompressionquality ( 100 );
				$img->setImageFormat ( "jpg" );
				$img->writeImage ( $_tmp_savefile_name );
			}
			
			// 处理文件
			if ($size ['width']) {
				$model_img = new Cms_ImageDispose ();
				
				// 设置图片质量
				$model_img->setImgDisplayQuality ( 100 );
				$model_img->setImgCreateQuality ( 100 );
				
				$model_img->setSrcImg ( $_tmp_savefile_name );
				$model_img->setCutType ( 1 );
				$model_img->setDstImg ( $savefile_name );
				if ($model_img->createImg ( $size ['width'] )) {
					return $savefile_name;
				}
			} else {
				if ($src_fileinfo ['ext'] == "png" || $src_fileinfo ['ext'] == "bmp") {
					return $_tmp_savefile_name;
				} else {
					move_uploaded_file ( $_tmp_savefile_name, $savefile_name );
				}
				$savefile_name = $this->preview_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
				return $savefile_name;
			}
		} elseif ($this->upload_type == 'URL') {
			ob_start ();
			readfile ( $src_filename );
			$img_string = ob_get_contents ();
			ob_end_clean ();
			
			$src_fileinfo = mime_contenttype ( $src_filename );
			$preview_filename = $this->preview_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
			
			// 将PNG图片转换，放置在待处理文件夹内
			if ($src_fileinfo ['ext'] == "png" || $src_fileinfo ['ext'] == "bmp") {
				$_tmp_savefile_name = $this->tmp_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
				$fp2 = @fopen ( $_tmp_savefile_name, "a" );
				fwrite ( $fp2, $img_string );
				fclose ( $fp2 );
				$preview_filename = $this->savepath . DIRECTORY_SEPARATOR . $this->save_filename . '.jpg';
				// 转换图片格式
				$img = new Imagick ( $_tmp_savefile_name );
				$img->setImageFormat ( "jpg" );
				$img->writeImage ( $this->tmp_filesave_path . DIRECTORY_SEPARATOR . $this->save_filename . '.jpg' );
			}
			
			if ($size ['width']) {
				$imagedispose_model = new Cms_ImageDispose ();
				// 设置图片质量
				$imagedispose_model->setImgCreateQuality ( 100 );
				$imagedispose_model->setImgDisplayQuality ( 100 );
				if ($imagedispose_model->setImageSrc ( $img_string, 'STRING', $src_fileinfo ['ext'] )) {
					$imagedispose_model->setCutType ( 1 );
					$imagedispose_model->setDstImg ( $preview_filename );
					$img_info = $imagedispose_model->createImg ( $size ['width'] );
					if ($img_info == 'ok') {
						return $preview_filename;
					}
				}
			} else {
				$fp2 = @fopen ( $preview_filename, "a" );
				fwrite ( $fp2, $img_string );
				fclose ( $fp2 );
				return $preview_filename;
			}
		}
		return false;
	}
	
	/**
	 * 新闻上传照片
	 */
	function uploadContent($src_filename, $size = null) {
		
		if ($this->upload_type == 'local') {
			if (! file_exists ( $src_filename )) {
				return false;
			}
		}
		
		// 判断是否为图片
		$src_fileinfo = mime_contenttype ( $src_filename );
		if (substr ( $src_fileinfo ['mime'], 0, 5 ) != 'image') {
			return false;
		}
		$savefile_name = $this->savepath . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
		
		// 生成目标存储目录
		if (! file_exists ( $this->savepath )) {
			makeDir ( $this->savepath );
		}
		if (! $size) {
			if (! copy ( $src_filename, $savefile_name )) {
				return false;
			}
		} 		// 将原图缩放
		else {
			// ......
		}
		
		// 插入附件
		$DB_Attachments = new DB_Attachments ();
		$attach_id = $DB_Attachments->insert ( array (
				"uid" => $this->session_info ['uid'],
				"resource_id" => $this->resource_id,
				"attach_type" => $this->attachment_type,
				"ext" => $src_fileinfo ['ext'],
				"description" => "新闻原图",
				"save_name" => $this->save_filename,
				"save_location" => str_replace ( DIRECTORY_SEPARATOR, '/', $this->hashpath ),
				"filesize" => filesize ( $savefile_name ),
				"filemd5" => md5_file ( $savefile_name ),
				"ctime" => date ( "Y-m-d H:i:s" ) 
		) );
		
		// 生成100*70缩放图像
		$model_img = new Cms_ImageDispose ();
		$model_img->setSrcImg ( $savefile_name );
		$model_img->setCutType ( 3 );
		$savefile_name_100 = $this->savepath . DIRECTORY_SEPARATOR . $this->save_filename . '_100.' . $src_fileinfo ['ext'];
		$model_img->setDstImg ( $savefile_name_100 );
		if ($model_img->createImg ( 100, 70 )) {
			$attach_id = $DB_Attachments->insert ( array (
					"uid" => $this->session_info ['uid'],
					"resource_id" => $this->resource_id,
					"attach_type" => $this->attachment_type,
					"ext" => $src_fileinfo ['ext'],
					"description" => "新闻图片 100x70",
					"save_name" => $this->save_filename . '_100',
					"save_location" => str_replace ( DIRECTORY_SEPARATOR, '/', $this->hashpath ),
					"filesize" => filesize ( $savefile_name ),
					"filemd5" => md5_file ( $savefile_name ),
					"ctime" => date ( "Y-m-d H:i:s" ) 
			) );
		}
		
		if ($attach_id) {
			$DB_News = new DB_News ();
			$DB_News->updateByPrimary ( array (
					'news_img' => str_replace ( DIRECTORY_SEPARATOR, '/', $this->hashpath ) . '/' . $this->save_filename . '.' . $src_fileinfo ['ext'] 
			), $this->resource_id );
			return $this->hashpath . DIRECTORY_SEPARATOR . $this->save_filename . '.' . $src_fileinfo ['ext'];
		}
		
		return false;
	}
}

?>